using NL;
using NL.Combat;
using NL.MiyaAssetBundle;
using NL.UI;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using UnityEngine.UI;

namespace XGame.UI
{
    public class WinBattleMain : Window
    {
    	public Text level_txt;
    
    	public Text chapter_process_txt;

    	public Text chapter_txt;

    	public GameObject level_obj;

    	public GameObject chapter_obj;

    	public Image process_img;

    	public Button pause_btn;

        public GameObject message_type1_obj;

        public GameObject message_type2_obj;

        public GameObject boss_bar;
        public Image boss_hp_process_img;
        public Image boss_hp_process1_img;
        private float boss_hp_delay_time_count = -1;

        private List<MessageData> messageList;

        public class MessageData
        {
            public int type;
            public string str1;
            public string str2;
        }

    	public void SetProcess(float percent){
    		process_img.fillAmount = percent;
            time_count = 2;
    	}

    	public void SetLevel(int level){
    		if (level_txt!=null){
    			level_txt.text = "Lv." + level;
    		}
            time_count = 2;
    	}

    	public void SetChapterProcess(string str){
			if (chapter_process_txt!=null){
    			chapter_process_txt.text = str;
    		}
    	}

    	public void SetChapterInfo(string str){
			if (chapter_txt!=null){
    			chapter_txt.text = str;
    		}
    	}

    	public void ShowLevelPanel(){
    		if (level_obj!=null){
    			level_obj.SetActive(true);
    		}
    		if (chapter_obj!=null){
    			chapter_obj.SetActive(false);
    		}
    	}

    	public void ShowChapterPanel(){
    		if (chapter_obj!=null){
    			chapter_obj.SetActive(true);
    		}
    		if (level_obj!=null){
    			level_obj.SetActive(false);
    		}
    	}

        public void ShowBossBar(float percent)
        {
            boss_bar.SetActive(true);
            boss_hp_process_img.fillAmount = percent;
            if (boss_hp_delay_time_count == -1)
            {
                boss_hp_delay_time_count = 0.3f;
            }
        }

        public void HideBossBar()
        {
            boss_bar.SetActive(false);
        }
        
        public void ShowMessageBar(int type=1, string str1="", string str2="")
        {
            if(messageList==null)
            {
                messageList = new List<MessageData>();
            }

            MessageData data = new MessageData();
            data.type = type;
            data.str1 = str1;
            data.str2 = str2;

            messageList.Add(data);

            if(message_time_count > -1)
            {
                return;
            }

            ShowNextMessage();
        }

        private void ShowNextMessage()
        {
            if(messageList.Count > 0)
            {
                MessageData data = messageList[0];
                if (data.type == 1)
                {
                    message_type1_obj.SetActive(true);
                    message_type1_obj.transform.Find("1").GetComponent<Text>().text = data.str1;
                    message_type1_obj.transform.Find("2").GetComponent<Text>().text = data.str2;
                }
                else if (data.type == 2)
                {
                    message_type2_obj.SetActive(true);
                    message_type2_obj.GetComponentInChildren<Text>().text = data.str1;
                }
                message_time_count = 3;
            }
        }

		protected override void Start(){
            base.Start();

            message_type1_obj.SetActive(false);
            message_type2_obj.SetActive(false);
            boss_bar.SetActive(false);

            if (pause_btn != null)
            {
                pause_btn.onClick.AddListener(() =>
               {
                   _ONClick?.Invoke();
               });
            }
        }

        Action _ONClick;
        public WinBattleMain WaitPauseClick(Action _on)
        {
            _ONClick = _on;
            return this;
        }

        private float time_count = -1;
        private float message_time_count = -1;
        private void Update()
        {
            if (time_count > -1)
            {
                time_count -= Time.deltaTime;

                if (time_count <= 0)
                {
                    time_count = -1;
                    ShowChapterPanel();
                }
            }

            if (message_time_count > -1)
            {
                message_time_count -= Time.deltaTime;

                if (message_time_count <= 0)
                {
                    message_time_count = -1;
                    message_type1_obj.SetActive(false);
                    message_type2_obj.SetActive(false);
                    messageList.RemoveAt(0);
                    ShowNextMessage();
                }
            }

            if (boss_hp_delay_time_count > -1)
            {
                boss_hp_delay_time_count -= Time.deltaTime;

                if (boss_hp_delay_time_count<=0)
                {
                    boss_hp_delay_time_count = -1;
                    boss_hp_process1_img.fillAmount = boss_hp_process_img.fillAmount;
                }

            }

        }

    }
}